<!--
 * @Autor: Gct
 * @Description: 网站资讯
 * @Date: 2022-04-18 19:00:50
 * @LastEditors: Gct
 * @LastEditTime: 2022-04-18 22:07:37
-->
<template>
  <div>
    <!-- 第一行开始 -->
    <el-row>
      <el-col :span="18">
        <el-form
          size="small"
          :inline="true"
          :model="params"
          class="demo-form-inline"
        >
          <el-form-item>
            <el-select
              clearable
              v-model="params.status"
              placeholder="请选择状态"
            >
              <el-option label="审核通过" value="审核通过"></el-option>
              <el-option label="审核不通过" value="审核不通过"></el-option>
              <el-option label="未审核" value="未审核"></el-option>
              <el-option label="推荐" value="推荐"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              clearable
              v-model="params.title"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select
              clearable
              v-model="params.categoryId"
              placeholder="请选择栏目"
            >
              <el-option
                v-for="item in categoryListPro"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="6" style="text-align: right">
        <el-button type="primary" size="small" @click="toPublish">发布咨询</el-button>
      </el-col>
    </el-row>
    <!-- 第一行结束 -->
    <!-- 第二行表格开始 -->
    <el-table :data="articleList" style="width: 100%">
      <el-table-column
        type="index"
        :index="1"
        label="序号"
        width="80"
        align="cneter"
      >
      </el-table-column>
      <el-table-column prop="cover" label="封面" width="160" align="cneter">
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="" width="150px" />
        </template>
      </el-table-column>
      <el-table-column prop="address" label="风采" width="680">
        <template slot-scope="scope">
          <div>{{ scope.row.title }}</div>
          <div v-if="scope.row.baseUser">
            {{ scope.row.baseUser.realname }}
          </div>
          <div>{{ scope.row.category.name }}</div>
          <div>{{ scope.row.publishTime | fmtTime }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="cneter">
      </el-table-column>
      <el-table-column label="操作" align="cneter"> </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="recommed(scope.row.id)"
            >推荐</el-button
          >
          <el-button type="text" @click="check(scope.row)">审核</el-button>
          <el-button type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 第二行表格结束 -->
    <!-- 第三行分页开始 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.page"
      :page-sizes="[5, 15, 20, 30]"
      :page-size="params.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 第三行分页结束 -->
    <!-- 模态框开始 -->
    <el-dialog title="审核风采信息" :visible.sync="visible" width="60%">
      <!-- 显示文章标题 -->
      <div style="text-align: center; font-size: 26px">
        {{ article.title }}
      </div>
      <!-- 显示文章内容 -->
      <div v-html="article.content"></div>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="danger"
          @click="toChangeStatus(article.id, '审核不通过')"
          >审核不通过</el-button
        >
        <el-button
          type="primary"
          @click="toChangeStatus(article.id, '审核通过')"
          >审核通过</el-button
        >
      </span>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>
<script>
import {
  pageQuery,
  changeStatus,
  saveOrUpdate,
  findAll,
} from "@/api/category.js";
export default {
  data() {
    return {
      // 分页条件表单对象
      params: {
        page: 1,
        pageSize: 10,
      },
      // 文章列表
      articleList: [],
      // 列表总条数
      total: 0,
      // 所有文章分类
      categoryListPro: [],
      // 接收文章当前行的数据
      article: {},
      visible: false,
    };
  },
  created() {
    this.findAllCategory();
    this.findAllCateList();
  },
  watch: {
    params: {
      handler() {
        this.findAllCategory();
      },
      deep: true,
    },
  },
  methods: {
    /**
     * @description: 分页并根据条件获取资讯信息
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    async findAllCategory() {
      let temp = { ...this.params };
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key];
        }
      }
      let res = await pageQuery(temp);
      this.articleList = res.data.list;
      this.total = res.data.total;
    },

    /**
     * @description: 处理分页条数变化的函数
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.page = 1;
    },

    /**
     * @description: 处理分页的函数
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    handleCurrentChange(page) {
      this.params.page = page;
    },

    /**
     * @description: 所有文章分类
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    async findAllCateList() {
      let res = await findAll({
        page: 1,
        pageSize: 1000,
      });
      this.categoryListPro = res.data;
    },

    /**
     * @description: 给搜索按钮绑定事件
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    toSearch() {
      this, this.findAllCateList();
    },

    /**
     * @description:
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    recommed(id) {
      this.$confirm("此操作将推荐该咨询, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 确认推荐
          let res = await changeStatus({ id, status: "推荐" });
          this.$message({
            type: "success",
            message: res.message,
          });
          // 重载数据
          this.findAllCategory();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消推荐",
          });
        });
    },

    /**
     * @description: 审核文章
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    check(row) {
      this.visible = true;
      this.article = row;
    },

    /**
     * @description:修改文章状态
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    async toChangeStatus(id, status) {
      let res = await changeStatus({ id, status });
      this.$message({
        type: "success",
        message: res.message,
      });
      this.visible = false;
      this.findAllCategory();
    },

  /**
   * @description: 文章发布页面
   * @param {*}
   * @return {*}
   * @author: Gct
   */    
  toPublish(){
    // 编程式导航 跳转文章详情页面
    this.$router.push({
      path:"/toPubDetails/index",
    })
  }
  },
};
</script>
<style lang="scss">
.el-dialog__header {
  background-color: #2cb5ac;
  .el-dialog__title {
    color: white;
  }
}
.el-dialog__body {
  border-bottom: 2px solid #2cb5ac;
}
</style>